<?php

// +----------------------------------------------------------------------
// | 用户
// +----------------------------------------------------------------------
// | Copyright (c) 2015-2024 http://www.yicmf.com, All rights reserved.
// +----------------------------------------------------------------------
// | Author: 微尘 <yicmf@qq.com>
// +----------------------------------------------------------------------

namespace app\controller\api\v3;

use app\event\ucenter\User as UserEvent;
use app\model\index\CreditcardProduct as CreditcardProductModel;
use app\pay\event\UserBalance as UserBalanceEvent;
use app\pay\event\UserBalanceCash as UserBalanceCashEvent;
use app\model\pay\PointProduct as PointProductModel;
use app\model\ucenter\UserMoneyLog as UserMoneyLogModel;
use app\model\ucenter\UserCash as UserCashModel;
use app\model\index\Order as OrderModel;
use app\model\ucenter\UserTask as UserTaskModel;
use app\model\ucenter\MemberInfo as MemberInfoModel;
use app\event\index\Task as TaskEvent;
use app\event\pay\PointOrder as PointOrderEvent;
use think\facade\Db;

class Task extends Common
{
    public function index()
    {

        if ($this->user) {
            $info['yesterday_profit'] = UserMoneyLogModel::where('money', '>', 0)
                ->where('user_id', $this->user['id'])
                ->whereTime('create_time', 'yesterday')
                ->where('type', 'in', [11, 12, 13, 14, 15, 16, 18, 20, 21, 66])->sum('money');
            $info['all_cash'] = -UserMoneyLogModel::where('user_id', $this->user['id'])
                ->where('type', 'in', [1])->sum('money');

            return $this->successAjax($info);
        } else {
            return $this->errorAjax('用户未登录');
        }

    }

    public function game()
    {
        $data['user_id'] = $this->user['id'];
        $data['title'] = '游戏试玩';
        sleep(2);
        return $this->successAjax($data);
    }

    public function lists()
    {

        $status = input('status/d', -1);
        $sheet_id = input('sheet_id/d', 0);

        //订单列表
//        $OrderList =  OrderModel::field('id,productName,createtime,order_no,product_type,product_id,status')
//            ->order('id desc')
//            ->paginate($limit)->each(function ($item){
//                if($item['product_type'] == 1 || $item['product_type'] == 3){
//                    $item['logo'] = $this->request->domain(). Db::name('creditcard_product')->where('id',$item['product_id'])->value('logo');
//                }elseif($item['product_type'] == 4 ){
//                    $item['logo'] = '../../assets/yxzq.png';
//                }elseif ($item['product_type'] == 5 ){
//                    $item['logo'] = '../../assets/zyjd.png';
//                }else{
//                    $product_order = Db::name('point_order')->where('orderId',$item['id'])->find();
//                    $item['logo'] = Db::name('point_product')->where('id',$product_order['self_id'])->value('logo');
//                }
//                return $item;
//            });
        //订单列表
        $data['lists'] = UserTaskModel::where('user_id', $this->user['id'])
            ->when($sheet_id, function ($query) use ($sheet_id) {
                $query->where('task_type', $sheet_id);
            })
            ->when($status != -1, function ($query) use ($status, $sheet_id) {
                if ($sheet_id == 0) {
                    $query->where('status', $status);
                } else {
                    $query->where('step', $status);
                }
            })
            ->order('id desc')
            ->append(['status_text', 'status_color'])
            ->where('id', '>', 397)
            ->paginate();
        //状态:0=进行中,1=已提交,2=已完结,3=已失效,4=已驳回
        $data['sheets'][] = ['name' => '全部',
            'status' => [
                [
                    'id' => -1,
                    'name' => '全部'
                ], [
                    'id' => 0,
                    'name' => '已提交'
                ],
                [
                    'id' => 1,
                    'name' => '进行中'
                ],
                [
                    'id' => 2,
                    'name' => '已完结'
                ],
                [
                    'id' => 3,
                    'name' => '已失败'
                ],
                [
                    'id' => 4,
                    'name' => '已驳回'
                ]
            ]];
        $data['sheets'][] = ['name' => '信用卡',
            'status' => [
                [
                    'id' => -1,
                    'name' => '全部'
                ], [
                    'id' => 0,
                    'name' => '访问'
                ],
                [
                    'id' => 1,
                    'name' => '通过'
                ],
                [
                    'id' => 3,
                    'name' => '未提交'
                ],
                [
                    'id' => 4,
                    'name' => '审核中'
                ],
                [
                    'id' => 5,
                    'name' => '待激活'
                ],
                [
                    'id' => 4,
                    'name' => '审核中'
                ],
                [
                    'id' => 5,
                    'name' => '拒绝'
                ],
                [
                    'id' => 6,
                    'name' => '拒绝'
                ],
                [
                    'id' => 7,
                    'name' => '二卡'
                ],
                [
                    'id' => 8,
                    'name' => '已激活'
                ],
                [
                    'id' => 10,
                    'name' => '提交失败'
                ],
                [
                    'id' => 11,
                    'name' => '待首刷'
                ],
                [
                    'id' => 12,
                    'name' => '已注销'
                ],
                [
                    'id' => 13,
                    'name' => '无效'
                ],
                [
                    'id' => 20,
                    'name' => '降级通过'
                ]
            ]];
        $data['sheets'][] = ['name' => '贷款',
            'status' => [
                [
                    'id' => -1,
                    'name' => '全部'
                ], [
                    'id' => 0,
                    'name' => '已提交'
                ],
                [
                    'id' => 1,
                    'name' => '进行中'
                ],
                [
                    'id' => 2,
                    'name' => '已完结'
                ],
                [
                    'id' => 3,
                    'name' => '已拒绝'
                ]
            ]];
        $data['sheets'][] = ['name' => '租机',
            'status' => [
                [
                    'id' => -1,
                    'name' => '全部'
                ], [
                    'id' => 0,
                    'name' => '已提交'
                ],
                [
                    'id' => 1,
                    'name' => '进行中'
                ],
                [
                    'id' => 2,
                    'name' => '已完结'
                ],
                [
                    'id' => 3,
                    'name' => '已拒绝'
                ]
            ]];
        return $this->successAjax($data);
    }

    public function old()
    {

        $status = input('status/d', 0);
//        $where['user_id'] = $this->user['id'];
        if ($status == 1) {
            $where['status'] = ['in', [1, 3]];
        } else if ($status != -1) {
            $where['status'] = $status;
        }

        $status = input('status/d', 0);
        $limit = input('limit/d', 15);

        //订单列表
        $OrderList = OrderModel::when($status != 0, function ($query) use ($status) {
            if ($status == 1) {
                $query->where('status', -1);
            } elseif ($status == 2) {
                $query->where('status', 0);
            } elseif ($status == 3) {
                $query->where('status', 'in', [1, 3]);
            } elseif ($status == 4) {
                $query->where('status', 2);
            } else {
                $query->where('status', $status);
            }
        })
            ->where('user_id', $this->user['id'])
            ->field('id,productName,createtime,order_no,product_type,product_id,status')
            ->order('id desc')
            ->paginate($limit)->each(function ($item) {
                if ($item['product_type'] == 1 || $item['product_type'] == 3) {
                    $product = Db::name('creditcard_product')->where('id', $item['product_id'])->find();
                    $item['logo'] = 'http://admin.mayichuangye168.com' . $product['logo'];
                    $product_order = Db::name('creditcard_order')->where('orderId', $item['id'])->find();
                    $item['apply_name'] = $product_order['userName'];
//					$item['apply_tel'] = substr_replace($product_order['userTel'],'****',3,4);
                    $item['apply_tel'] = secrecy_show($product_order['userTel']);

                } elseif ($item['product_type'] == 4) {
                    $item['logo'] = '../../../assets/yxzq.png';
                    $item['apply_name'] = '--';
                    $item['apply_tel'] = '--';
                } elseif ($item['product_type'] == 5) {
                    $item['logo'] = '../../../assets/zyjd.png';
                    $item['apply_name'] = '--';
                    $item['apply_tel'] = '--';
                } else {
                    $product_order = Db::name('point_order')->where('orderId', $item['id'])->find();
                    if ($product_order) {
                        $item['logo'] = Db::name('point_product')->where('id', $product_order['self_id'])->value('logo');
                        $item['apply_name'] = '--';
                        $item['apply_tel'] = '--';
                    }

                }
                return $item;
            });
        //订单列表
        $data['lists'] = $OrderList;

        return $this->successAjax($data);
    }

    public function detail()
    {
        $CreditCardList = CreditcardProductModel::where('id', input('id'))
//                ->order('sort desc,id desc')
            ->field('*,applyNum as apply_num,applyMsg as apply_msg,settlementRules as settlement_rules,bankName as name,interestMsg as interest_msg')->find();
        $level = MemberInfoModel::where('id', '<', 4)->field('id,fee,type')->select()->toArray();

        foreach ($level as $index => $item) {
            $level[$index]['money'] = round($CreditCardList['commission'] * $item['fee'], 2);
        }

        $data['detail'] = $CreditCardList;
        $data['hot'] = CreditcardProductModel::where(['status' => '1', 'type' => 1])
//                ->order('sort desc,id desc')
            ->field('id,applyNum as apply_num,bankName as name,type,commission_type,logo,label1,label2,shortIntroduce,settlementType,commission')
            ->order('id desc')->limit(3)->select();
        $data['level_info'] = $level;
        return $this->successAjax($data);
    }

    public function point()
    {

        if ($this->request->isPost()) {
            $event = new PointOrderEvent();
            $data = $event->add($this->request->param(),$this->user);
            return $this->autoAjax($data);
        } else {
            $CreditCardList = PointProductModel::where('id', input('id'))->find();
            $level = MemberInfoModel::where('id', '<', 4)->field('id,fee,type')->select()->toArray();
            $data['detail'] = $CreditCardList;
            $data['hot'] = PointProductModel::where('status', 1)->limit(3)->orderRand()->select();
            $data['level_info'] = $level;
            $data['time_start'] = 8;
            $data['time_tips'] =  '积分客服工作时间8:00-22:00';
            $data['time_end'] =  22;
            return $this->successAjax($data);
        }
    }

    /**
     * 提交任务
     */
    public function add()
    {
        if ($this->request->isPost()) {
            $event = new TaskEvent();
            $data = $event->add($this->request->param(), $this->user);
        } else {
            $data['code'] = 1;
            $data['message'] = '请求错误';
        }
        return $this->autoAjax($data);

    }
}
